﻿#ifndef __SS_ASIO_SESSION_H__
#define __SS_ASIO_SESSION_H__
#include <boost/asio.hpp>
#include <boost/thread.hpp>
//#include "../../StarSeekerBaseLib/ssBaseDefine.h"
#include "time/ssTime.h"
#include "container/ssBuffer.h"
#include "message/ssNetMsgHead.h"


#ifndef SS_ASIO_PACK_SIZE
#define SS_ASIO_PACK_SIZE		10240
#endif

namespace StarSeeker
{

enum asioClose_e:int
{
	CLOSE_NORMAL=0,
	CLOSE_BY_READ,
	CLOSE_BY_WRITE,
	CLOSE_BY_ACCEPT,
	CLOSE_BY_READBUF,
	CLOSE_BY_ACCEPT_FULL,
	CLOSE_BY_READ_FAST,
};

class ssAsioNetWork;

class ssAsioSession
{
	friend class ssAsioNetWork;
public:
	ssAsioSession(void);
	virtual ~ssAsioSession(void);
private:
	//初始化
	bool Init(u32 id, boost::shared_ptr<boost::asio::ip::tcp::socket>& socket, u32 maxBuffSize, ssAsioNetWork* pNetMgr);
	//收包间隔检测 @启用功能 @间隔时间（毫秒）@临界次数
	void SetTimeCheck(bool timeCheck, i64 intervalTime = 100, u16 checkCount = 20);
	//获取Socket对象
	boost::shared_ptr<boost::asio::ip::tcp::socket>& GetSocket();
	//获取ID
	u32 GetID() { return m_sessionID; }
	//关闭socket
	void Close(asioClose_e errType, i32 err);
protected:
	//收到后的处理
	void OnReceive();
	//分析数据包
	bool ParseData();


	bool					m_timeCheck;	//是否检测收包间隔
	i64						m_intervalTime;	//收包间隔,毫秒
	i64						m_lastTime;		//上次收包时间,毫秒
	u16						m_fastCount;	//频繁次数,超过20次断线
	u16						m_checkCount;	//超过这个次数就触发事件
	boost::shared_ptr<boost::asio::ip::tcp::socket>	m_socket;
	ssBuffer				m_recvBuf;		//接收缓存
	u32						m_sessionID;	//连接编号,0表示无效的
	std::string				m_ip;
	ssAsioNetWork*			m_pNetMgr;
};
typedef boost::shared_ptr<ssAsioSession> ssAsioSessionPtr;

}
#endif
